package com.zkdcloud.repository;

import com.zkdcloud.domain.Product;
import org.jfaster.mango.annotation.DB;
import org.jfaster.mango.annotation.SQL;
import org.jfaster.mango.plugin.page.Page;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 商品
 *
 * @author zk123
 * @version 2018/5/22
 */
@DB(table = "product")
@Repository
public interface ProductDao {
    String COLUMNS = "id,business_id,category_id,name,price,description,image_url,month_sales,rate,left_num,create_time,status";

    /**
     * 增加product
     *
     * @param product product
     */
    @SQL("insert into #table ("+COLUMNS+") values(:id,:businessId,:categoryId,:name,:price,:description," +
            ":imageUrl,:monthSales,:rate,:leftNum,:createTime,:status)")
    void add(Product product);

    /**
     * 根据businessId获取商品
     *
     * @param bid bid
     * @return product
     */
    @SQL("select * from #table where business_id = :1")
    List<Product> getAllByBid(String bid,Page page);

    /**
     * 根据id 获取
     *
     * @param cid cid
     * @return list<product></product>
     */
    @SQL("select * from #table where category_id = :1")
    List<Product> getAllByCid(String cid);
    /**
     * 根据id 获取
     *
     * @param cid cid
     * @return list<product></product>
     */
    @SQL("select * from #table where category_id = :1")
    List<Product> getAllByCid(String cid, Page page); /**
     * 根据id 获取
     *
     * @param cid cid
     * @return list<product></product>
     */
    @SQL("select * from #table where category_id = :1 and status = 1")
    List<Product> getAvaliByCid(String cid, Page page);

    /**
     * 根据id获取
     *
     * @param id id
     * @return product
     */
    @SQL("select * from #table where id = :1")
    Product getOne(String id);

    /**
     * 删除指定的id
     *
     * @param id id
     */
    @SQL("delete from #table where id = :1")
    void remove(String id);

    /**
     * 修改指定的产品
     *
     * @param product product
     */
    @SQL("update #table set id = :id" +
            " #if(:categoryId != null) ,category_id = :categoryId  #end" +
            " #if(:name != null) ,name = :name  #end" +
            " #if(:price != null) ,price = :price  #end" +
            " #if(:description != null), description = :description  #end" +
            " #if(:imageUrl != null), image_Url = :imageUrl  #end" +
            " #if(:leftNum != null) ,left_Num = :leftNum  #end" +
            " #if(:monthSales != null) ,month_sales = :monthSales  #end" +
            " #if(:status != null) ,status = :status  #end" +
            " where id = :id")
    void update(Product product);
}
